clear all
clc
close all

R = 100;
C = 33e-6;

RC = R * C;
tau = RC / (2*pi);

Tmod = 10*RC;
T = RC/1000;

t = 0:T:Tmod;

% E  = 0*(t<(0*Tmod/4)) + 1*(t>=(0*Tmod/4));
%  E = 13*randn(1,length(t));

% f = 100;
% A =1;
% E = A*cos(2*pi*f*t);



fmin = 1/(Tmod/2);
fmax = 2/RC/2/pi;

f = fmin:((fmax-fmin)/100):fmax;
A = 1;
K = nan(1,length(f));

for j=1:length(f)
    E = A*sin(2*pi*f(j)*t);
    
    U = nan(1,length(t));
    dU = nan(1,length(t));
    
    
    
    U(1)=E(1);
    dU(1)=0;
    U2 = U;
    
    for k=2:length(t);
        U(k) = U(k-1) + dU(k-1)*T;
        dU(k) = (E(k)-U(k))/ RC;
        
        U2(k) = (RC*U2(k-1) + E(k)*T)/(RC + T);
        
    end
    
    Us = U(fix(end/2):end);
    K(j) = (max(Us)-min(Us))/2;
    
end

figure(1)
plot(f,20*log10(K));
% axis([fmin fmax 0 1]);

% plot(t,E,t,U,t,U2);
% xlabel('t,sec');
% ylabel('U,V');
% legend('E','U','U2');
